<template>
  <t-dialog :header="header" :visible.sync="visible" :onClose="closeDialog"     :closeOnOverlayClick="false"
            @confirm="onSubmit" :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
            width="1000px" top="5%">
    <div style="height: 65vh">
      <t-form ref="form" :data="formData" :colon="true" labelWidth="150px" :rules="rules">
        <t-row style="row-gap: 24px">
          <t-col :span="12">
            <t-form-item label="内容（标题/作者/来源/收录）" name="content" labelAlign="top">
              <t-textarea
                clearable
                placeholder="请输入内容"
                :autosize="{ minRows: 8 }"
                :maxlength="3000"
                v-model="formData.content"
              ></t-textarea>
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="类别" name="type">
              <t-input clearable v-model="formData.type" placeholder="请输入类别" style="width: 95%"></t-input>
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="期刊影响因子" name="influence">
              <t-input-number
                v-model="formData.influence"
                theme="normal"
                :min="0"
                placeholder="请输入期刊影响因子"
              ></t-input-number>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="高被引论文（是否）" name="hot">
              <t-radio-group v-model="formData.hot">
                <t-radio :value="false">否</t-radio>
                <t-radio :value="true">是</t-radio>
              </t-radio-group>
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
export default {
  name: 'edit-add-expert-result',
  props: ['objId', 'expertId', 'header'],
  data() {
    return {
      visible: true,
      loading: false,
      formData: {
        expertId: this.expertId,
        content: undefined,
        type: undefined,
        influence: undefined,
        hot: false,
      },
      rules: {
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类别不能为空' }],
        influence: [{ required: true, message: '期刊影响因子不能为空' }],
      },
    };
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      },
    },
  },
  methods: {
    getObj() {
      this.$api.expert
        .expertAchievementGet(this.objId)
        .then((res) => {
          this.formData = res.data;
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    closeDialog() {
      this.$emit('update:visible', false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId !== 0) {
            this.$api.expert
              .expertAchievementEdit(this.formData)
              .then(() => {
                this.$message.success('修改成功');
                this.$emit('reload');
                this.$emit('update:visible', false);
                this.loading = false;
              })
              .catch((e) => {
                this.loading = false;
                this.$message.error(e.toString());
              });
          } else {
            this.$api.expert
              .expertAchievementAdd(this.formData)
              .then(() => {
                this.$message.success('新增成功');
                this.$emit('reload');
                this.$emit('update:visible', false);
                this.loading = false;
              })
              .catch((e) => {
                this.loading = false;
                this.$message.error(e.toString());
              });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .t-form__label{
  display: block !important;
}
</style>
